package cn.zy.jboa.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.zy.jboa.dao.CommonDAO;
import cn.zy.jboa.entity.BizClaimVoucher;
import cn.zy.jboa.service.BizClaimVoucherService;

@Service("bizClaimVoucherService")
public class BizClaimVoucherServiceImpl implements BizClaimVoucherService {

	@Autowired
	CommonDAO commonDao;
	
	public void setCommonDao(CommonDAO commonDao) {
		this.commonDao = commonDao;
	}

	@Override
	public List claimList(String id, String position, int first, int size,
			String startDate, String endDate,String status) {
		StringBuffer hql=new StringBuffer("from BizClaimVoucher bv LEFT JOIN FETCH bv.sysEmployeeByCreateSn where 1=1");
		if(StringUtils.isNotBlank(startDate)){
			hql.append(" and '"+startDate+"'<= bv.createTime ");
		}
		if(StringUtils.isNotBlank(endDate)){
			hql.append(" and '"+endDate+"'>= bv.createTime ");
		}
		if(StringUtils.isNotBlank(status)){
			hql.append(" and '"+status+"'=bv.status");
		}
		if("员工".equals(position)){
			hql.append(" and bv.sysEmployeeByCreateSn.sn='"+id+"'");
		}else{
			hql.append(" and bv.sysEmployeeByNextDealSn.sn='"+id+"' and bv.status != '新创建'");
		}
		hql.append(" order by bv.createTime desc");
		return (List) commonDao.query(hql.toString(), first, size);
	}

	@Override
	public int getCount(String sn, String position, String startDate,
			String endDate , String status) {
		StringBuffer hql=new StringBuffer("select count(id) from BizClaimVoucher");
		if("员工".equals(position)){
			hql.append(" where sysEmployeeByCreateSn.sn='"+sn+"'");
		}else{
			hql.append(" where sysEmployeeByNextDealSn.sn='"+sn+"' and status != '新创建'");
		}
		if(StringUtils.isNotBlank(startDate)){
			hql.append(" and '"+startDate+"'<= createTime ");
		}
		if(StringUtils.isNotBlank(endDate)){
			hql.append(" and '"+endDate+"'>= createTime ");
		}
		if(StringUtils.isNotBlank(status)){
			hql.append(" and '"+status+"'=status");
		}
		int count = Integer.parseInt(commonDao.count(hql.toString()).toString());
		return count ;
	}

	@Override
	public BizClaimVoucher getId(int id) {
		BizClaimVoucher bizClaimVoucher = (BizClaimVoucher) commonDao.get(BizClaimVoucher.class, id);
		return bizClaimVoucher;
	}

	@Override
	public void delId(int id) {
		commonDao.del(BizClaimVoucher.class, id);
	}

	@Override
	public void updateCV(BizClaimVoucher bcv) {
		commonDao.update(bcv);
	}

	@Override
	public void add(BizClaimVoucher claimVoucher) {
		commonDao.add(claimVoucher);
	}

}
